var DEFAULT_ADJUST_AMT = 2.75;
var CHECKMARK = '\u2718';

var TOAST_TPAD = 12;
var TOAST_HGHT = 198;

function _bb_block_thissite(link) {

    if (link)
        _bb_handle_pref_link(link);

}

function _bb_block_allsites(link) {

    if (link)
        _bb_handle_pref_link(link);

}

function _bb_nowarn_thissite(link) {

    if (link)
        _bb_handle_pref_link(link);

}

function _bb_nowarn_allsites(link) {

    if (link)
        _bb_handle_pref_link(link);

}

function _bb_handle_pref_link(link) {

    if (link.isChecked)
        return;

    link.isChecked = true;

    link.normalize();

    var checkmarkSpan = document.createElement('span');
    checkmarkSpan.appendChild(document.createTextNode(CHECKMARK + ' '));
    checkmarkSpan.appendChild(
        document.createTextNode(link.childNodes[0].nodeValue));

    with (checkmarkSpan.style) {
        color = '#000';
        paddingLeft = paddingRight = '3px';
        paddingTop = paddingBottom = '1px';
        backgroundColor = '#E0E8E0';
    }

    link.style.display = 'none';
    link.parentNode.insertBefore(checkmarkSpan, link.nextSibling)

}

function _bb_toast_hide(div) {

    if (arguments.length == 0) {
        _bb_toast_hide(document.getElementById('_bb_toast'));
        return;
    }

    _bb_toast_show(div, -1*TOAST_TPAD, -1*TOAST_HGHT);

}

function _bb_toast_show(div, topPaddingRemaining, heightRemaining) {

    if (arguments.length == 0) {
        _bb_toast_show(document.getElementById('_bb_toast'), TOAST_TPAD,
            TOAST_HGHT);
        return;
    }

    var callback = null;

    var targetProp = null;
    var nextShowCall = null;

    var adjustAmount = DEFAULT_ADJUST_AMT;

    if (heightRemaining >= 0 && topPaddingRemaining != 0) {

        if (topPaddingRemaining < 1)
            adjustAmount *= -1;

        targetProp = 'paddingTop';

        var nextTopPaddingRemaining = topPaddingRemaining - adjustAmount;
        if ( (adjustAmount > 1 && nextTopPaddingRemaining < 0) ||
             (adjustAmount < 1 && nextTopPaddingRemaining > 0) ) {
            adjustAmount += nextTopPaddingRemaining;
            nextTopPaddingRemaining = 0;
        }

        nextShowCall = function() {
            _bb_toast_show(div, nextTopPaddingRemaining, heightRemaining);
        };

    }
    else if (heightRemaining != 0) {

        if (heightRemaining < 1)
            adjustAmount *= -1;

        targetProp = 'height';

        var nextHeightRemaining = heightRemaining - adjustAmount;
        if ( (adjustAmount > 1 && nextHeightRemaining < 0) ||
             (adjustAmount < 1 && nextHeightRemaining > 0) ) {
            adjustAmount += nextHeightRemaining;
            nextHeightRemaining = 0;
        }

        nextShowCall = function() {
            _bb_toast_show(div, topPaddingRemaining, nextHeightRemaining);
        };

    }

    if (targetProp != null) {

        var curValStr = div.style[targetProp];
        curValPx = parseFloat(curValStr.substring(0, curValStr.length-2));
        if (!curValPx) curValPx = 0;

        callback = function() {
            div.style[targetProp] = (curValPx + adjustAmount) + 'px';
            nextShowCall();
        };

    }

    if (callback)
        setTimeout(callback, 1);

}
